package com.simulation.superlotto.common.utils;

import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author ：XXX
 * @date ：Created in 2019/12/9
 * @description ：
 * @version: 1.0
 */
public class TimeUtils {
    public static Date getDate(String s)
    {
        Date parse=new Date();
        try {
            parse = new SimpleDateFormat("yyyy-MM-dd").parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }
    /**
     * @desc 字符串转时间戳
     * @param String time
     * @example time="2019-04-19 00:00:00"
     **/
    public static Long getTimestamp(String time) {

        Long timestamp = null;
        try {
            timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timestamp;
    }/**
     * @desc 字符串转时间戳
     * @param String time
     * @example time="2019-04-19 00:00:00"
     **/
    public static Long getTimestampDate(String time) {

        Long timestamp = null;
        try {
            timestamp = new SimpleDateFormat("yyyy-MM-dd").parse(time).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timestamp;
    }
    /**
     * @desc  时间戳转字符串
     * @param Long timestamp
     * @example timestamp=1558322327000
     **/
    public static String getStringTime(Date timestamp) {
        String datetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timestamp);
        // 获取年月日时分秒
        return datetime;
    }    /**
     * @desc  时间戳转字符串
     * @param Long timestamp
     * @example timestamp=1558322327000
     **/
    public static String getOrderNum(Date timestamp) {
        String datetime = new SimpleDateFormat("yyyyMMddHHmmss").format(timestamp);
        // 获取年月日时分秒
        return datetime;
    }

    /**
     * @desc  时间戳转字符串
     * @param Long timestamp
     * @example timestamp=1558322327000
     **/
    public static String getStringTimeDate(Date timestamp) {
        String date = new SimpleDateFormat("yyyy-MM-dd").format(timestamp);
        // 获取只有年月日的时间
        String datetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timestamp);
        // 获取年月日时分秒
        return date;
    }
    /**
     * @desc  时间戳转字符串
     * @param Long timestamp
     * @example timestamp=1558322327000
     **/
    public static String getStringMonth(Date timestamp) {
        String date = new SimpleDateFormat("yyyy-MM").format(timestamp);
        // 获取只有年月日的时间
        String datetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timestamp);
        // 获取年月日时分秒
        return date;
    }
    public static String getStringMonthDay(Date timestamp) {
        String dateDay = new SimpleDateFormat("yyyy-MM-dd").format(timestamp);
        return dateDay;
    }
    //获取当前时间前几天或者后几天
    public static String getDate(Date date,int i) {
        //SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, i);
        date = calendar.getTime();

        return getStringTime(date);
    }


    //获取当前时间前几个月或者后几个月
    public static String getDateMonth(Date date,int i) {
        //SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, i);
        date = calendar.getTime();

        return getStringMonth(date);
    }
    //获取两个时间段相差年份
    public static Integer getAge(String date) {
        Integer year=0;
        SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd" );
        Calendar calendar = Calendar.getInstance();
        Calendar instance = Calendar.getInstance();
        calendar.setTime(new Date());
        try {
        instance.setTime(sdf.parse(date));
             year = calendar.get(Calendar.YEAR) - instance.get(Calendar.YEAR);
            Integer month = calendar.get(Calendar.MONTH) - instance.get(Calendar.MONTH);
            if(month>=6){
                year++;
            }
           // System.out.println(year+"---"+month);
        } catch (ParseException e) {
            e.printStackTrace();
        }
          return year;
    }






    public static void main(String[] args) {
//        String date = "1019-02-20 13:48:33";
//        Integer year=0;
//        SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
//        Calendar calendar = Calendar.getInstance();
//        Calendar instance = Calendar.getInstance();
//        calendar.setTime(new Date());
//        try {
//            instance.setTime(sdf.parse(date));
//            year = calendar.get(Calendar.YEAR) - instance.get(Calendar.YEAR);
//            Integer month = calendar.get(Calendar.MONTH) - instance.get(Calendar.MONTH);
//            System.out.println(year+"---"+month);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }

        //System.out.println(age);
    }


    public static int testBetweenDays(String s){

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        long time = 0;
        long time1 = 0;
        try {
            time = sdf.parse(s).getTime();
            time1 =sdf.parse( getStringTimeDate(new Date())).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }


        long l = (time1 - time) / (1000 * 3600 * 24);
        return (int)l;
    }


    public static int testBetweenDays2(String s1,String s2){

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        long time = 0;
        long time1 = 0;
        try {
            time = sdf.parse(s1).getTime();
            time1 =sdf.parse(s2).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }


        long l = (time1 - time) / (1000 * 3600 * 24);
        return (int)l;
    }
}
